package JavaMetrics;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * A basic implementation of ILineCounter which can be used to process C, C++,
 * and JAVA source files ("common" source files).
 * @author MANISH SHARMA & Ekkehard Blanz
 */
public class CommonCounter {


    /** Creates a new instance of CommonCounter.  This counter overwrites the
     * countline method of its parent with one that not only recognizes single
     * line comments but also those that can span several lines.
     */
    public CommonCounter() {
    }

    /** Counts net LOCs, blank lines, comment lines and gross LOCs in "common"
     * files.
     *
     * @return BasicFileInfo object containing filename and all counts
     *
     *
     */

    public BasicFileInfo countlines(String filename){

        return this.countlines(new File(filename));
    }

    public BasicFileInfo countlines(File file){
        BufferedReader br = null;
        try {
            int fsource = 0;
            int fcomments = 0;
            int fblanks = 0;
            boolean inComment;
            String data = null;
            br = new BufferedReader(new FileReader(file));
            inComment = false;
            while ((data = br.readLine()) != null) {
                if (inComment) {
                    if (data.indexOf("*/") == -1) {
                        fcomments++;
                        continue;
                    } else if (data.trim().endsWith("*/")) {
                        fcomments++;
                        inComment = false;
                        continue;
                    } else {
                        fsource++;
                        inComment = false;
                        continue;
                    }
                }
                if (data.trim().startsWith("//")) {
                    fcomments++;
                    continue;
                }
                if (data.trim().equals("")) {
                    fblanks++;
                    continue;
                }
                if (data.trim().startsWith("/*")) {
                    if (data.trim().endsWith("*/") || data.indexOf("*/") == -1) {
                        fcomments++;
                    } else {
                        fsource++;
                    }
                } else {
                    fsource++;
                }
                if (data.indexOf("/*") != -1 && data.indexOf("*/") == -1) {
                    inComment = true;
                }
            }
            br.close();
            BasicFileInfo ob = new BasicFileInfo();
            ob.setFblanks(fblanks);
            ob.setFcomments(fcomments);
            ob.setFname(file.toString());
            ob.setFsourcelines(fsource);
            ob.setFtotal(fblanks + fcomments + fsource);

            return ob;
        } catch (Exception ex) {
            return null;
        } finally {
            try {
                br.close();
            } catch (Exception ex) {
            }
        }
    }
}

